package com.kevin.util.log;

import java.io.PrintStream;

import lombok.extern.log4j.Log4j;

/**
 * 控制台输出重定向到log4j
 * 
 * @ClassName StdOutErrRedirect
 * @author <a href="mailto:donggongai@126.com" target="_blank">kevin</a>
 * @date 2016年9月26日 下午2:30:30
 *
 */
@Log4j
public class StdOutErrRedirect {

    public static void redirectSystemOutAndErrToLog() {
        PrintStream printStreamForOut = createLoggingWrapper(System.out, false);
        PrintStream printStreamForErr = createLoggingWrapper(System.out, true);
        System.setOut(printStreamForOut);
        System.setErr(printStreamForErr);
    }

    public static PrintStream createLoggingWrapper(final PrintStream printStream, final boolean isErr) {
        return new PrintStream(printStream) {
            @Override
            public void print(String string) {
                if (!isErr) {
                    log.warn(string);
                } else {
                    log.error(string);
                }
            }

            @Override
            public void print(boolean b) {
                if (!isErr) {
                    log.warn(Boolean.valueOf(b));
                } else {
                    log.error(Boolean.valueOf(b));
                }
            }

            @Override
            public void print(char c) {
                if (!isErr) {
                    log.warn(Character.valueOf(c));
                } else {
                    log.error(Character.valueOf(c));
                }
            }

            @Override
            public void print(int i) {
                if (!isErr) {
                    log.warn(String.valueOf(i));
                } else {
                    log.error(String.valueOf(i));
                }
            }

            @Override
            public void print(long l) {
                if (!isErr) {
                    log.warn(String.valueOf(l));
                } else {
                    log.error(String.valueOf(l));
                }
            }

            @Override
            public void print(float f) {
                if (!isErr) {
                    log.warn(String.valueOf(f));
                } else {
                    log.error(String.valueOf(f));
                }
            }

            @Override
            public void print(double d) {
                if (!isErr) {
                    log.warn(String.valueOf(d));
                } else {
                    log.error(String.valueOf(d));
                }
            }

            @Override
            public void print(char[] x) {
                if (!isErr) {
                    log.warn(x == null ? null : new String(x));
                } else {
                    log.error(x == null ? null : new String(x));
                }
            }

            @Override
            public void print(Object obj) {
                if (!isErr) {
                    log.warn(obj);
                } else {
                    log.error(obj);
                }
            }

        };
    }
}